﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class TeleportationManager : MonoBehaviour, IResettable {

    private List<Vector3> positions = new List<Vector3>();

    private int targetPosition = 0;
    private float legLength = 0;
    private float legTotalTime = 0;
    private float legCurrentTime = 0;

    private bool teleporting = false;
    private ParticleSystem teleportParticles;

    public float teleportMoveSpeed = 20.0f;

	public GameObject teleportSound;

    private Transform player;

	// Use this for initialization
	void Start () {
        teleportParticles = particleSystem;
        teleportParticles.enableEmission = false;
        ResetItemList.Instance.AddItem(this);
        player = GameObject.FindGameObjectWithTag("Player").transform;
        FollowActionPriorityQueue.Instance.AddFollowPosition(new TeleportEffectPrioritizedFollowPosition(this));
	}
	
	// Update is called once per frame
	void Update () {

        if (teleporting)
        {
            teleportParticles.enableEmission = true;

            legCurrentTime += Time.deltaTime;
            this.transform.position = Vector3.Lerp(positions[targetPosition - 1], positions[targetPosition], legCurrentTime / legTotalTime);
            if (this.transform.position == positions[targetPosition])
            {
                StartNextLeg();
            }
        }
        else
        {
            teleportParticles.enableEmission = false;
        }

	}

    internal void Execute(System.Collections.Generic.List<Vector3> bouncePositions)
    {
        // Can't teleport if the player is being destroyed right now
        if (player.GetComponent<DeathDetection>().isBeingDestroyed) return;

		audio.Play();

        // Add player's current position to the movement list
        positions.Add(player.transform.position);
        foreach (Vector3 position in bouncePositions)
        {
            positions.Add(position);
        }

        if (positions.Count >= 2)
        {
            teleporting = true;
            player.renderer.enabled = false;
            teleportParticles.enableEmission = true;
            teleportParticles.Play();
            StartNextLeg();
        }
        else
        {
            print("Not enough positions");
        }
    }

    private void StartNextLeg()
    {
        if (targetPosition == positions.Count - 1)
        {
            teleporting = false;
            player.renderer.enabled = true;
            player.position = positions[targetPosition];
            ResetItemList.Instance.RemoveItem(this);
            Destroy(this.gameObject);
        }
        else
        {
            targetPosition++;
            //print("leg " + targetPosition + " from " + positions[targetPosition - 1] + " to " + positions[targetPosition]);
            legLength = Vector3.Distance(positions[targetPosition - 1], positions[targetPosition]);
            // v = m / s; s = m / v
            legTotalTime = legLength / teleportMoveSpeed;
            legCurrentTime = 0;
        }

    }

    public void Reset()
    {
        player.renderer.enabled = true;
        Destroy(this.gameObject);
    }

    bool destroyed = false;
    public bool IsDestroyed { get { return destroyed; } }

    void OnDestroy()
    {
        destroyed = true;
        ResetItemList.Instance.RemoveItem(this);
    }

    public bool PersistAfterReset()
    {
        return false;
    }
}
